#include <iostream>
#include <queue>
using namespace std;

const int kMaxA = 1200;
const int kMaxB = 1200;

int val[kMaxA][kMaxB];
int min_val[kMaxA][kMaxB], max_val[kMaxA][kMaxB];

int Solve(int a, int b, int n) {
  for (int i = 0; i < a; i++) {
    deque<int> min_deq, max_deq;
    for (int j = 0; j < n - 1; j++) {
      for (; !min_deq.empty() && val[i][min_deq.back()] >= val[i][j]; min_deq.pop_back());
      min_deq.push_back(j);
      for (; !max_deq.empty() && val[i][max_deq.back()] <= val[i][j]; max_deq.pop_back());
      max_deq.push_back(j);
    }
    for (int j = n - 1; j < b; j++) {
      for (; !min_deq.empty() && val[i][min_deq.back()] >= val[i][j]; min_deq.pop_back());
      min_deq.push_back(j);
      for (; !max_deq.empty() && val[i][max_deq.back()] <= val[i][j]; max_deq.pop_back());
      max_deq.push_back(j);
      min_val[i][j - n + 1] = val[i][min_deq.front()];
      max_val[i][j - n + 1] = val[i][max_deq.front()];
      // cout << i << ", " << j - n + 1 << ": min=" << min_val[i][j - n + 1] << " max=" << max_val[i][j - n + 1] << endl;
      if (max_deq.front() + n - 1 <= j) {
        max_deq.pop_front();
      }
      if (min_deq.front() + n - 1 <= j) {
        min_deq.pop_front();
      }
    }
  }

  int ans = 1e9;
  for (int j = 0; j <= b - n; j++) {
    deque<int> min_deq, max_deq;
    for (int i = 0; i < n - 1; i++) {
      for (; !min_deq.empty() && min_val[min_deq.back()][j] >= min_val[i][j]; min_deq.pop_back());
      min_deq.push_back(i);
      for (; !max_deq.empty() && max_val[max_deq.back()][j] <= max_val[i][j]; max_deq.pop_back());
      max_deq.push_back(i);
    }
    for (int i = n - 1; i < a; i++) {
      for (; !min_deq.empty() && min_val[min_deq.back()][j] >= min_val[i][j]; min_deq.pop_back());
      min_deq.push_back(i);
      for (; !max_deq.empty() && max_val[max_deq.back()][j] <= max_val[i][j]; max_deq.pop_back());
      max_deq.push_back(i);
      ans = min(ans, max_val[max_deq.front()][j] - min_val[min_deq.front()][j]);

      if (max_deq.front() + n - 1 <= i) {
        max_deq.pop_front();
      }
      if (min_deq.front() + n - 1 <= i) {
        min_deq.pop_front();
      }
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int a, b, n;

  cin >> a >> b >> n;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      cin >> val[i][j];
    }
  }
  cout << Solve(a, b, n) << "\n";
  return 0;
}